fs.ques = (function(){

	var quiz, player, lang;
	var currId, bypass = new Array();

	var sortedQuiz	= new Array(),
		storage		= new Array();

	var pass = false;

	return {

		getItem: function(k)	{ return fs.storage.getItem('quiz-' + k);	},
		setItem: function(k, v)	{ fs.storage.setItem('quiz-' + k, v);		},
		removeRecord: function(){
			$.each(sortedQuiz, function( k, v ) { fs.storage.removeItem('quiz-' + parseInt(v.mapId, 10)); });
		},

		quizStatusCheck : function(){

			if ( $('[role="quiz"]').length > 0 ) return false;

			var exam = this.getQuiz( fs.video.getPos() );

			if ( exam ) {
				this.setupQuestion( exam );
				fs.video.pause();
			}

			return true;
		},

		init : function( p, q, l ){

			quiz = q; player = p; lang = l;
			if ( quiz.length == 0 ) return;

			if ( fs.video.type() != 'html5' ) {

				if ( !fs.video.isSupport('html5') ) {
					alert( lang.quizNoSupFlashPS );
					fs.video.forcePlayerType( 'html5', lang.quizNeedHTML5PlayerPS );

					return;
				}

				fs.video.changePlayer('html5');

				return;
			}

			fs.video.forcePlayerType( 'html5', lang.quizNeedHTML5PlayerPS );

			this.sortQuiz();
			this.bindEvent();

			var cuePoint = new Array();

			var self = this;

			$.each(quiz, function( k, v ){
				cuePoint.push( {evt: 'video-quiz', time: parseInt(v.time, 10)/1000, args: { id : k }} );
			});

			fs.video.setBeforeSeek( $.proxy(this.checkQuiz, this) );
			fs.video.setCuePoint( cuePoint );
		},

		sortQuiz : function(){

			var qt = new Array();
			for (var key in quiz) qt.push( quiz[key] );

			qt.sort(function(a, b) {return a.time - b.time});

			$.each(qt, function( k, v ) { sortedQuiz.push(v); });
		},

		bindEvent: function(){

			var self = this,
				exam = self.getQuiz( fs.video.getPos() );

			if ( exam ) this.setupQuestion( exam );

			fs.event.register('player.timeUpdate', function(){
				if ( $('[role="quiz"]').length > 0 ) fs.video.pause();
			});

			fs.event.register('video-quiz', function(obj){

				if ( $('[role="quiz"]').length > 0 ) return;
				self.quizStatusCheck();
			});
		},

		checkQuiz : function( time ) {

			if ( pass ) {
				pass = false;
				return {status: true};
			}

			var ct = fs.video.getPos();

			if ( Math.abs(time-ct) < .5 ) return {status: false};

			if ( $('[role="quiz"]').length > 0 && time < ct ) {
				this.hideQuiz();
				return {status: true};
			}

			bypass = new Array();
			$.each(sortedQuiz, function( k, v ) {
				if ( (parseInt(v.time, 10) / 1000) >= time ) return;

				var config	= JSON.parse( v.config ),
					mapId	= parseInt(v.mapId, 10);

				if ( config.bypass != 'true' ) return;

				bypass.push(mapId);
			});

			var q = this.getQuiz( time, true );

			return ( q )
				? {status:false, pause:true, pos: parseInt(q.time, 10)/1000}
				: {status:true};
		},

		getQuiz : function( ct ){
			var q		= null,
				self	= this;

			$.each(sortedQuiz, function( k, v ) {

				if ( (parseInt(v.time, 10) / 1000) > ct ) return;

				var config	= JSON.parse( v.config ),
					mapId	= parseInt(v.mapId, 10);

				if ( config.bypass == 'true' && $.inArray(mapId, bypass) != -1 ) return;

				if ( self.getItem( mapId ) != 'true' ) {
					q = v;
					return false;
				}
			});

			return q;
		},

		setupQuestion : function( exam ){

			currId	= exam.mapId;

			var id		= 'quiz-' + currId,
				ques	= fs.ques[ exam.type ];

			var question = $('<div>', {
				'id'	: id,
				'role'	: 'quiz',
				'class'	: 'quiz ' + exam.type,
				'css'	: {'display': 'none'},
				'html'	: ques.init( id, exam, lang )
			});

			$('#' + player).append( $('<div>', {'role' : 'quizMask', 'class' : 'quiz-mask'}) );
			$('#' + player).append( question );

			question.show('fast', function(){
				ques.setupEvent();

				pass = true;
				fs.event.registerOne('player.seekFinished', function(){ pass = false; });

				fs.video.setPos( parseInt(exam.time, 10) / 1000 );
			});

			question.bind('touchmove', function(e){ e.stopPropagation(); });
		},

		minimize : function( id ){
			var question = $('#' + id);

			var max = $('<button>', {
				'class' : 'maxbtn btn btn-mini',
				'text'	: lang.quizResizer,
				'role'	: 'maximize'
			});

			$('#' + player).append( max );

			question.addClass('minimize');

			var self = this;
			max.click( function(){ self.maximize( id ); } );
		},

		maximize : function( id ){
			var question = $('#' + id);

			$('#' + player).find('[role="maximize"]').remove();

			question.removeClass('minimize');
		},

		hideQuiz : function(){
			currId = null;

			var question 	= $('[role="quiz"]'),
				self		= this;

			question.hide('fast', function() {
				$(this).remove();
				$('[role="quizMask"]').remove();
				$('[role="maximize"]').remove();
			} );
		},

		next : function(){

			this.setItem( currId, true );
			currId = null;

			var question	= $('[role="quiz"]'),
				self		= this;

			question.hide('fast', function() {
				$(this).remove();

				var q = self.getQuiz( fs.video.getPos() );
				if (q) {
					self.setupQuestion(q);
					return;
				}

				$('[role="quizMask"]').remove();
				fs.video.play();
			});
		}
	}
})();
